package com.baizhi.hospital.service.impl;

import com.baizhi.hospital.dao.ExamDao;
import com.baizhi.hospital.entity.Exam;
import com.baizhi.hospital.service.ExamService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Service
public class ExamServiceImpl implements ExamService {
	private Logger logger = LoggerFactory.getLogger(ExamServiceImpl.class);

	@Resource
	private ExamDao examDao;
	@Override
	public Map<String, Object> insertExam(Exam exam) {
		HashMap<String, Object> map = new HashMap<>();
//		设置属性
		String examId  = UUID.randomUUID().toString().replace("-","");
		logger.info(examId);
		exam.setExamId(examId);
		exam.setExamStartDate(LocalDateTime.now());
		int i = examDao.insertExam(exam);
		if(i > 0){
			map.put("isSuccess","提交审核成功");
		}else{
			map.put("isSuccess","提示审核失败");
		}
		return map;
	}

	@Override
	public Map<String, Object> selectExamPage(int page, int limit, String name) {
		if(name!=null && !"".equals(name)){
			name = "%"+name+"%";
		}else{
			name=null;
		}
		int offset = (page-1)*limit;

		List<Exam> exams = examDao.selectExamPage(offset, limit, name);
		int count = examDao.selectCount(name);
		HashMap<String, Object> map = new HashMap<>();
		map.put("data",exams);
		map.put("count",count);
		map.put("code",0);
		map.put("msg","");
		return map;
	}

	@Override
	public Map<String, Object> updateExam(Exam exam) {
		HashMap<String, Object> map = new HashMap<>();
		int i = examDao.updateExam(exam);

		if(i > 0){
			map.put("isSuccess","感谢审核");
		}else{
			map.put("isSuccess","对不起，操作失败");
		}
		return map;
	}
}
